<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Abstract
 *
 * @author Kam
 */
abstract class My_DAO_Abstract {

    protected $dbTableMap = array();
    protected $dbTableName;
    protected static $dbTables = array();
    protected static $translator;
    
    public function quoteInto($text, $value) {
        return $this->getDbTable()->getAdapter()->quoteInto($text, $value);
    }

    protected function setSafety(&$src, $dst, $destKey, $srcKey = null) {
        if ($srcKey == null) {
            $srcKey = $destKey;
        }
        if (isset($dst[$destKey]) || array_key_exists($destKey, $dst)) {
            $src[$srcKey] = $dst[$destKey];
        }
        return $src;
    }

    public function get($id) {
        return $this->getDbTable()->get($id);
    }

    public function getByColumn($val, $columnName) {
        return $this->getDbTable()->getByColumn($val, $columnName);
    }

    public function getByCode($code) {
        return $this->getDbTable()->getByCode($code);
    }

    public function translate($key) {
        $arg_list = func_get_args();
        $msg = self::getTranslator()->_($key);
        if (count($arg_list) > 1) {
            array_splice($arg_list, 0, 1);
            $msg = sprintf($msg, $arg_list[0]);
        }
        return $msg;
    }

    public function _($key, array $data = null) {
        $msg = self::getTranslator()->_($key);
        if ($data != null && !empty($data)) {
            $tmpkeys = array_keys($data);
            $vals = array_values($data);
            $keys = array();
            foreach ($tmpkeys as $tmpkey) {
                $keys[] = '%' . $tmpkey . '%';
            }
            $msg = str_replace($keys, $vals, $msg);
        }
        return $msg;
    }

    protected static function getTranslator() {
        if (self::$translator == null) {
            self::$translator = Zend_Registry::get('Zend_Translate');
            ;
        }
        return self::$translator;
    }

    /**
     * 
     * @param string $name
     * @return My_Db_Table
     */
    public function getDbTable($name = null) {
        if ($name == null) {
            $name = $this->getDefaultDbTableName();
        }
        $qname = null;
        $dbTableNameMap = $this->getDbTableNameMap();
        if (isset($dbTableNameMap[$name])) {
            $qname = $dbTableNameMap[$name];
            if (isset(self::$dbTables[$qname])) {
                return self::$dbTables[$qname];
            }
            $dbTable = new $qname();
            if ($dbTable instanceof Zend_Db_Table_Abstract) {
                self::$dbTables[$qname] = $dbTable;
                return $dbTable;
            }
        }
        if (isset(self::$dbTables[$name])) {
            return self::$dbTables[$name];
        }
        throw new My_Exception('No DB Table registered');
    }

    protected abstract function getDefaultDbTableName();

    protected abstract function getDbTableNameMap();
}

